import os.path
import platform


def get_sys():
    """
    获得当前的系统名字
    :return: Windows/Linux/Mac/Other
    """
    system = platform.system()
    if system == "Windows":
        return 'Windows'
    elif system == "Linux":
        return 'Linux'
    elif system == "Darwin":
        return 'Mac'
    else:
        return 'Other'


if __name__ == '__main__':
    # 获得当前路径
    curPath = os.path.abspath('.')
    # 将路径定位到根路径下的node_modules的.bin中
    modulesPath = os.path.join(curPath, '..', '..', 'node_modules', '.bin')
    modulesPath = os.path.abspath(modulesPath)
    # 获得当前的系统平台,根据不同平台进行指令变化
    curSys = get_sys()
    PROTOC_GEN_TS_PATH = ''
    PROTOC_GEN_GRPC_PATH = ''
    if curSys == 'Windows':
        PROTOC_GEN_TS_PATH = os.path.join(modulesPath, "protoc-gen-ts.cmd")
        PROTOC_GEN_GRPC_PATH = os.path.join(modulesPath, "grpc_tools_node_protoc_plugin.cmd")
    elif curSys == 'Linux':
        PROTOC_GEN_TS_PATH = os.path.join(modulesPath, "protoc-gen-ts")
        PROTOC_GEN_GRPC_PATH = os.path.join(modulesPath, "grpc_tools_node_protoc_plugin")

    # 输出路径
    OUT_DIR = '.'

    genCmd = (f'protoc '
              f'--plugin=protoc-gen-ts="{PROTOC_GEN_TS_PATH}" '
              f'--plugin=protoc-gen-grpc="{PROTOC_GEN_GRPC_PATH}" '
              f'--js_out=import_style=commonjs,binary:"{OUT_DIR}" '
              f'--ts_out=service=grpc-node,mode=grpc-js:"{OUT_DIR}" '
              f'--grpc_out=grpc_js:"{OUT_DIR}" '
              f'*.proto'
              )

    os.system(genCmd)
